Seaborn is a Python Visualization library based on Matplotlib. It provides a high-level interface for creative statistical graphics. Seaborn simplifies the process of generating complex visualizations and is particularly useful for creating aesthetically pleasing plots with minimal code.
Typical use cases for Seaborn include data visualization in statistical analysis, machine learning, and data exploration tasks.
Plotly is a Python graphing library that makes interactive, publication-quality graphs online. It supports a wide range of chart types and is known for its interactivity and ease of use. Plotly allows for creating interactive dashboards and web-based visualizations.
Plotly is commonly used for data visualization in web applications, interactive reports, and data-driven presentations.
#seaborn - line plot
import seaborn as sns
import matplotlib.pyplot as plt
x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]
sns.lineplot(x = x,y = y)
plt.show()
• Strengths: High interactivity with hover effects, zooming, panning, and annotations.
• Weaknesses: Slightly more complex syntax for beginners, may require additional customization for aesthetics.
#plotly - line plot
import plotly.graph_objects as go
x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]
fig = go.Figure(data = go.Scatter(x=x, y=y, mode = 'lines'))
fig.update_layout(width=700, height=500)
fig.show()
#Seaborn - scatter plot
import seaborn as sns
import matplotlib.pyplot as plt
x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]
sns.scatterplot(x=x, y=y)
plt.show()
• Strengths: Excellent interactivity with tooltips, trend lines, and customizable markers.
• Weaknesses: Steeper learning curve, especially for beginners, due to its interactive nature.
#Plotly - scatter plot
import plotly.graph_objects as go
x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]
fig = go.Figure(data = go.Scatter( x=x, y=y, mode = 'markers'))
fig.update_layout(width=700, height=500)
fig.show()
#Seaborn - bar chart
import seaborn as sns
import matplotlib.pyplot as plt
categories = [ 'A','B','C','D' ]
values = [ 10,15,7,20 ]
sns.barplot( x = categories, y = values)
plt.show()
• Strengths: Highly interactive bar charts with hover effects, drill-down capabilities, and animations.
• Weaknesses: Requires additional configuration for static outputs, may be overkill for simple bar charts.
#Plotly - bar chart
import plotly.graph_objects as go
categories = [ 'A','B','C','D' ]
values = [ 10,15,7,20 ]
fig = go.Figure(data = go.Bar( x = categories, y= values))
fig.update_layout(width=700, height=500)
fig.show()
#Seaborn - histogram
import seaborn as sns
import matplotlib.pyplot as plt
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
sns.histplot(data, bins = 6)
plt.show()
• Strengths: Interactive histograms with bin selection, hover effects, and density estimation.
• Weaknesses: Requires understanding of binning and density concepts, may be overwhelming for beginners.
#Plotly - histogram
import plotly.figure_factory as ff
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
fig = ff.create_distplot([data], group_labels = ['Histogram'] , bin_size = 1)
fig.update_layout(width=700, height=600)
fig.show()
#seaborn - Pie Chart
#Seaborn dosen't directly supports pie charts, so we use matplotlib for this
import matplotlib.pyplot as plt
sizes = [20, 30, 15, 35]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels = labels, autopct = '%1.1f%%')
plt.show()
• Strengths: Highly customizable pie charts with interactive slices, labels, and colors.
• Weaknesses: Pie charts can be misleading, requires caution in usage for data representation.
#plotly - pie chart
import plotly.graph_objects as go
values = [20, 30, 15, 35]
labels = ['A', 'B', 'C', 'D']
fig = go.Figure(data = go.Pie(labels = labels, values = values))
fig.update_layout(width=500, height=600)
fig.show()
#Seaborn - Box Plot
import seaborn as sns
import matplotlib.pyplot as plt
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
sns.boxplot(data)
plt.show()
• Strengths: Interactive box plots with hover effects, quartile selection, and outlier highlighting.
• Weaknesses: Requires understanding of quartiles and outliers, may be complex for beginners.
#plotly - box plot
import plotly.graph_objects as go
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
fig = go.Figure(data=go.Box(y=data))
fig.update_layout(width=600, height=500)
fig.show()
#Seaborn - Violin Plot
import seaborn as sns
import matplotlib.pyplot as plt
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
sns.violinplot(data)
plt.show()
• Strengths: Interactive violin plots with hover effects, quartile selection, and kernel density estimation.
• Weaknesses: Requires understanding of kernel density estimation, may be challenging for beginners.
#plotly - Violin Plot
import plotly.graph_objects as go
data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]
fig = go.Figure(data = go.Violin(y= data))
fig.show()
#Seaborn - Heatmap
import seaborn as sns
import matplotlib.pyplot as plt
flights = sns.load_dataset('flights')
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')
sns.heatmap(flights_pivot, cmap='coolwarm', annot=True, fmt='d')
plt.show()
• Strengths: Interactive heatmaps with hover effects, annotations, and color scales.
• Weaknesses: Requires understanding of color scales and annotations, may be complex for beginners.
#Plotly - Heatmap
import plotly.express as px
import seaborn as sns
flights = sns.load_dataset('flights')
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')
fig = px.imshow(
flights_pivot,
color_continuous_scale='YlGnBu',
labels=dict(x="Year", y="Month", color="Passengers"),
title='Passenger Counts by Month and Year'
)
fig.update_layout(width=800, height=600)
fig.show()
#Seaborn dosen't directly support error barss in line plots; we may use Matplotlib for this
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 20, 12]
y_error = [1, 2, 1.5, 2, 1]
plt.errorbar(x,y, yerr = y_error, fmt = '-o')
plt.show()
• Strengths: Interactive error bars in line plots with hover effects, customizable error bar styles.
• Weaknesses: Requires understanding of error bar concepts, may be overwhelming for beginners
#Plotly
import plotly.graph_objects as go
x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 20, 12]
y_error = [1, 2, 1.5, 2, 1]
fig = go.Figure(data= go.Scatter(x=x , y=y, error_y = dict(type = 'data', array = y_error), mode = 'lines+markers'))
fig.update_layout(width=700, height=600)
fig.show()
#Seaborn dosen't directly support error barss in line plots; we may use Matplotlib for this
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(x, y, z)
plt.show()
• Strengths: Excellent support for 3D plotting with interactive rotations, zooming, and surface plots.
• Weaknesses: Requires understanding of 3D coordinate systems, may be complex for beginners.
#plotly - 3d
import plotly.graph_objects as go
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]
fig = go.Figure(data= [go.Scatter3d( x=x , y=y, z=z, mode = 'markers')])
fig.update_layout(width=700, height=600)
fig.show()
• A pair plot is a grid of pairwise plots that show the relationships between variables in a dataset. It displays scatter plots for numerical variables and histograms for distributions on the diagonal.
• Exploring relationships between multiple variables simultaneously.
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
sns.pairplot(iris, hue = 'species')
plt.show()
C:\Users\valan\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
sns.jointplot( x = 'total_bill', y = 'tip' , data = tips, kind = 'reg')
plt.show()
import seaborn as sns
import matplotlib.pyplot as plt
flights = sns.load_dataset('flights')
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')
sns.clustermap(flights_pivot, cmap = 'coolwarm', standard_scale = 1)
plt.show()
• A sunburst chart is a hierarchical chart that displays hierarchical data as a set of nested rings. Each ring represents a level in the hierarchy, with segments showing the proportion of each category.
• Visualizing hierarchical data structures, such as organizational hierarchies or nested categories.
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path = ['sex', 'day', 'time'], values = 'total_bill')
fig.update_layout(width=700, height=600)
fig.show()
import plotly.graph_objects as go
fig = go.Figure(go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = 'black', width = 0.5),
label=["A", "B", "C", "D", "E"],
),
link=dict(
source=[0, 1, 1, 0, 2],
target=[2, 3, 4, 4, 3],
value=[8, 4, 2, 8, 4],
),
))
fig.show()
• A choropleth map is a thematic map where areas are shaded or patterned in proportion to the value of a variable, such as population density or GDP per capita.
• Visualizing spatial data and geographical distributions, such as demographic trends or regional statistics.
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.choropleth(df, locations="iso_alpha", color="gdpPercap",
hover_name="country", color_continuous_scale=px.colors.sequential.Plasma)
fig.update_layout(width=800, height=700)
fig.show()